/*
 * Filename:    IntegrityArtificialScoreServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015年1月29日
 */
package com.paic.mhis.hcpms.trust.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.workflow.dto.HcpmsWFTaskDTO;
import com.paic.mhis.hcpms.common.workflow.vo.HcpmsWorkFlowVO;
import com.paic.mhis.hcpms.trust.biz.service.InsuredMedicalDetailService;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityArtificialScoreService;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityMedicalService;
import com.paic.mhis.hcpms.trust.biz.service.IntegrityWorkFlowService;
import com.paic.mhis.hcpms.trust.dto.IntegrityDoctorDTO;
import com.paic.mhis.hcpms.trust.dto.PointCutDTO;
import com.paic.mhis.hcpms.trust.intergration.dao.IntegrityWFDAO;
import com.paic.mhis.hcpms.trust.util.TrustConstants;

@Component("integrityArtificialScoreService")
public class IntegrityArtificialScoreServiceImpl implements IntegrityArtificialScoreService {
	@Autowired
	private IntegrityMedicalService integrityMedicalService;
	@Autowired
	IntegrityWorkFlowService integrityWorkFlowService;
	@Autowired
	private IntegrityWFDAO integrityWFDAO;
	@Autowired
	private InsuredMedicalDetailService insuredMedicalDetailService;
	@Autowired
	private HcpmsStorageService hcpmsStorageService;

	/**
	 * 撤回
	 */
	public Map<String, Object> backWarnProcess(String taskNo, String status, UserDetails user) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		// 启动
		Map<String, String> condition = new HashMap<String, String>();
		if (TrustConstants.STATUS_WAIT_AUDIT.equals(status)) {
			condition.put("AUDIT_OPINION", TrustConstants.INTEGRITY_OPINION_NO);
		}
		// 判断节点是否已经发生变化
		String actCurrent = integrityWorkFlowService.startWorkFlow(taskNo, user, condition);
		if (!actCurrent.equals(status)) {
			result.put(TrustConstants.STATE_KEY, TrustConstants.PROCESS_NOT_POWER);
			result.put(TrustConstants.MSG_KEY, TrustConstants.PROCESS_NOT_POWER_ERROR);
			return result;
		}
		// 流程
		HcpmsWorkFlowVO wfvo = new HcpmsWorkFlowVO();
		wfvo.setTaskNo(taskNo);
		wfvo.setActCurrent(user.getUsername());
		wfvo.setParamValue(condition);
		actCurrent = integrityWorkFlowService.processWorkFlow(wfvo);
		// 更新状态
		HcpmsWFTaskDTO taskDto = new HcpmsWFTaskDTO();
		taskDto.setTaskNo(taskNo);
		taskDto.setUpdatedBy(user.getUsername());
		taskDto.setActCurrent(actCurrent);
		taskDto.setAssigner(user.getUsername());
		taskDto.setAuthorizerType(TrustConstants.PERSON_TYPE);
		integrityWFDAO.updateWorkFlowTask(taskDto);
		result.put(TrustConstants.STATE_KEY, TrustConstants.SUCCESS_STATE_VALUE);
		result.put(TrustConstants.MSG_KEY, TrustConstants.WARN_PROCESS_REST_SUCCESS);
		return result;
	}

	/**
	 * 查询扣分信息
	 * 
	 * @param taskNo
	 * @param status
	 * @param user
	 * @return
	 * @throws Exception
	 */
	public Map<String, Object> integrityArtificialScoreInfo(String taskNo, UserDetails user) throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("caseType", TrustConstants.CASE_TYPE);
		paramMap.put("caseNo", taskNo);
		paramMap.put("userName", user.getUsername());
		List<PointCutDTO> pointList = integrityMedicalService.findPointCutStorageCollections(paramMap);
		result.put("pointList", pointList);
		List<IntegrityDoctorDTO> integrityList = insuredMedicalDetailService.queryIntegrityDoctorList(taskNo);
		result.put("integrityList", integrityList);
		result.put("integrityInfo", insuredMedicalDetailService.queryIntegrityDoctorByTaskNo(taskNo));
		return result;
	}

	/**
	 * 批量提交
	 * 
	 * @throws Exception
	 */
	public Map<String, Object> batchArtificialScore(JSONObject transferJson, UserDetails user) {
		Map<String, Object> result = new HashMap<String, Object>();
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		JSONArray batchArray = transferJson.getJSONArray("batchArtificialList");
		paramMap.put("status", TrustConstants.STATUS_WAIT_MODIFIED);
		paramMap.put("userInfo", user);
		paramMap.put("saveStatus", TrustConstants.STATUS_BATCH);
		int batchSize = batchArray.size();
		int systemError = 0;// 失败条数
		for (int i = 0; i < batchSize; i++) {
			paramMap.put("taskNo", batchArray.getJSONObject(i).getString("taskNo"));
			try {
				resultMap = integrityWorkFlowService.resultMapIntegrityWorkFlow(transferJson, paramMap, resultMap);
			} catch (Exception e) {
				systemError++;
				e.printStackTrace();
			}
			if (!resultMap.get(TrustConstants.STATE_KEY).equals(TrustConstants.SUCCESS_STATE_VALUE)) {
				systemError++;
			}
		}
		result.put(TrustConstants.STATE_KEY, TrustConstants.SUCCESS_STATE_VALUE);
		result.put(TrustConstants.MSG_KEY, TrustConstants.BATCH_SAVE_SUCCESS + "其中成功" + (batchSize - systemError)
				+ "条,失败" + systemError + "条");
		return result;
	}

}
